﻿using OpenCvSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViewROI;
using Image = System.Windows.Controls.Image;
using Point = System.Windows.Point;
using Rect = OpenCvSharp.Rect;

namespace ROIWindows.Views
{
    /// <summary>
    /// HWindowControl.xaml 的交互逻辑
    /// </summary>
    public partial class HWindowControl : UserControl
    {
        Image image = new Image();

        public Bitmap RenderMap
        {
            get
            {
                return renderMap;
            }
            set
            {
                renderMap = value;
                if (renderMap != null)
                {
                    image.Stretch = Stretch.Fill;
                    image.Width = canvas.ActualWidth;
                    image.Height = canvas.ActualHeight;
                    image.Source = BitmapManager.BitmapToBitmapSource(renderMap);
                }

                foreach (UIElement ui in canvas.Children)
                {
                    if (ui.GetType() == typeof(Image))
                    {
                        canvas.Children.Remove(ui);
                        break;
                    }
                }
                canvas.Children.Add(image);
            }
        }

        private Bitmap renderMap;

        List<Point[]> rects = new List<Point[]>();
        List<double> circlePara = new List<double>() { 0, 0, 0 };
        List<double> rectangle1Para = new List<double> { 0, 0, 0, 0 };
        List<double> linePara = new List<double>() { 0, 0, 0, 0 };

        public Canvas canvas = new Canvas();

        List<double> arrow = new List<double>();

        public HWindowControl()
        {
            InitializeComponent();
            this.Content = canvas;
            canvas.Background = new SolidColorBrush(Colors.Black);
            HDWindow = new HWindow();
            roiController = new ROIController();
            hWndCtrl = new HWndCtrl(this);
            hWndCtrl.useROIController(roiController);
            InitialEvents();
            mouseCoord = new Point();
        }
        /// <summary>
        /// 初始化ROI显示事件
        /// </summary>
        private void InitialEvents()
        {
            HDWindow.flush = () =>
            {
                this.Render();

                this.rects.Clear();
                this.arrow.Clear();
            };
            // 绘制矩形
            HDWindow.dispRRects += (rectPara) => {
                RotatedRect rotatedRect = new RotatedRect(new Point2f((float)rectPara[1],
                    (float)rectPara[0]), new Size2f((float)rectPara[3] * 2,
                    (float)rectPara[4] * 2), -(float)(rectPara[2] * 180 / Math.PI));

                Point2f[] point4 = rotatedRect.Points();
                Point[] normPoints = new Point[4];

                for (int i = 0; i < 4; i++)
                {
                    normPoints[i] = NormalizeCoord(point4[i]);
                }
                rects.Add(normPoints);
            };

            HDWindow.dispArrow += (_arrow) =>
            {
                arrow = _arrow;
            };

            HDWindow.dispCircle = (cir) =>
            {
                circlePara[0] = NormalizeCoord(new Point2f(cir[1], cir[0])).X;
                circlePara[1] = NormalizeCoord(new Point2f(cir[1], cir[0])).Y;
                //circlePara[2] = (cir[2] / this.ActualWidth * 2);
                circlePara[2] = cir[2];
            };

            HDWindow.drawRectangle1 = (rectangle1) =>
            {
                rectangle1Para[0] = NormalizeCoord(new Point2f((float)rectangle1[1], (float)rectangle1[0])).X;
                rectangle1Para[1] = NormalizeCoord(new Point2f((float)rectangle1[1], (float)rectangle1[0])).Y;
                rectangle1Para[2] = NormalizeCoord(new Point2f((float)rectangle1[3], (float)rectangle1[2])).X;
                rectangle1Para[3] = NormalizeCoord(new Point2f((float)rectangle1[3], (float)rectangle1[2])).Y;
            };

            HDWindow.dispLine = (line) =>
            {
                linePara[0] = NormalizeCoord(new Point2f(line[0], line[1])).X;
                linePara[1] = NormalizeCoord(new Point2f(line[0], line[1])).Y;
                linePara[2] = NormalizeCoord(new Point2f(line[2], line[3])).X;
                linePara[3] = NormalizeCoord(new Point2f(line[2], line[3])).Y;
            };

            canvas.MouseMove += (s, e) =>
            {
                mouseCoord = e.GetPosition(canvas);
            };
        }

        private Point NormalizeCoord(Point2f point)
        {
            Point normPoint = new Point();
            normPoint.X = point.X;
            normPoint.Y = point.Y;

            return normPoint;
        }

        public void Render()
        {
            List<UIElement> itemstoremove = new List<UIElement>();
            foreach (UIElement ui in canvas.Children)
            {
                if (ui.GetType() != typeof(Image))
                {
                    itemstoremove.Add(ui);
                }
            }
            foreach (UIElement ui in itemstoremove)
            {
                canvas.Children.Remove(ui);
            }

            GeometryPaint GP = new GeometryPaint(canvas, RenderMap);

            foreach (var points in rects)
            {
                GP.DrawAffineRectangle(points);
                if (rectangle1Para.Count > 0)
                {
                    GP.DrawRectangle1(rectangle1Para);
                }
            }

            if (circlePara.Count > 0)
            {
                GP.DrawCircle(circlePara);
            }

            if (linePara.Count > 0)
            {
                GP.DrawLine(linePara);
            }

            if (arrow.Count > 0)
            {
                GP.DrawArrow(arrow[0], arrow[1], arrow[2], arrow[3], 35);
            }

        }

      
        public HWindow HDWindow;
        public string HDID;
        public System.Drawing.Rectangle ImagePart;
        public ROIController roiController;
        public HWndCtrl hWndCtrl;
        Point mouseCoord;
    }
}

